package cn.livingCloud.controller.core.front;

import cn.livingCloud.dto.CaseCommentFormDto;
import cn.livingCloud.dto.CaseFrontInfoDto;
import cn.livingCloud.dto.JsonDto;
import cn.livingCloud.dto.front.*;
import cn.livingCloud.service.CaseService;
import cn.livingCloud.utils.BindingResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Controller
@RequestMapping("/front/cmc/case")
public class CaseFrontController {

    @Autowired
    private CaseService service;

    //案件举报
    @RequestMapping("/accusation.htm")
    public String edit(HttpServletRequest request, Model model) {
        CaseAccusationFrontFromDto formDto = service.loadFrontFromDto(request.getRequestURL().toString());
        model.addAttribute("formDto", formDto);
        return "front/cmc/case/accusation/accusation";
    }

    //举报提交
    @RequestMapping(value = "/accusation.htm", method = RequestMethod.POST)
    public String edit(@ModelAttribute("formDto") @Valid CaseAccusationFrontFromDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "front/cmc/case/accusation/accusation";
        }
        String uuid = service.accusationCaseFront(formDto);
        model.addAttribute("uuid", uuid);
        return "redirect:accusation_success.htm";
    }

    //跳转到举报成功界面
    @RequestMapping("/accusation_success.htm")
    public String accusationSuccess(String uuid, Model model) {
        model.addAttribute("uuid", uuid);
        return "front/cmc/case/accusation/accusation_success";
    }

    //案件作废
    @RequestMapping("/cancel")
    public String cancel(@RequestParam("uuid") String uuid, Model model) {
        CaseCancelFormFrontDto formDto = new CaseCancelFormFrontDto(uuid);
        model.addAttribute("formDto", formDto);
        return "front/cmc/case/cancel/cancel";
    }

    //案件作废
    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    @ResponseBody
    public JsonDto cancelSubmit(@Valid CaseCancelFormFrontDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return BindingResultUtils.buildErrorMessage(result, new JsonDto());
        }
        return service.cancelCase(formDto);
    }

    //案件处理成功后跳转界面
    @RequestMapping("/case_complete.htm")
    public String reportSuccess() {
        return "front/cmc/case/case_complete";
    }

    //上报案件
    @RequestMapping("/report")
    public String reportCaseForm(@RequestParam("uuid") String uuid, Model model) {
        CaseReportFrontFormDto formDto = new CaseReportFrontFormDto(uuid);
        model.addAttribute("formDto", formDto);
        return "front/cmc/case/report/report";
    }

    //上报案件
    @RequestMapping(value = "/report", method = RequestMethod.POST)
    @ResponseBody
    public JsonDto reportCaseSubmit(@Valid CaseReportFrontFormDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return BindingResultUtils.buildErrorMessage(result, new JsonDto());
        }
        return service.reportCase(formDto);
    }

    //完成案件
    @RequestMapping("/finish")
    public String finishForm(@RequestParam("uuid") String uuid, Model model) {
        CaseFinishFrontFormDto formDto = service.loadFinishFrontForm(uuid);
        model.addAttribute("formDto", formDto);
        return "front/cmc/case/finish/finish";
    }

    //完成案件
    @RequestMapping(value = "/finish", method = RequestMethod.POST)
    @ResponseBody
    public JsonDto finishForm(@Valid CaseFinishFrontFormDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return BindingResultUtils.buildErrorMessage(result, new JsonDto());
        }
        return service.finishCase(formDto);
    }

    //分派案件
    @RequestMapping("/assignment")
    public String assignmentForm(@RequestParam("caseUuid") String caseUuid, String deptUuid, Model model) {
        CaseAssignmentFrontFormDto formDto = service.loadAssignmentFrontForm(caseUuid, deptUuid);
        model.addAttribute("formDto", formDto);
        return "front/cmc/case/assignment/assignment";
    }

    //分派案件
    @RequestMapping(value = "/assignment", method = RequestMethod.POST)
    @ResponseBody
    public JsonDto assignmentSubmit(@Valid CaseAssignmentFrontFormDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return BindingResultUtils.buildErrorMessage(result, new JsonDto());
        }
        return service.assignmentCase(formDto);
    }

    //案件列表
    @RequestMapping("/list.htm")
    public String list(MyCaseFrontListDto listDto, Model model) {
        service.loadCaseList(listDto);
        model.addAttribute("listDto", listDto);
        return "front/cmc/case/list_case";
    }

    //案件列表-加载更多
    @RequestMapping("/load_more.htm")
    public String loadMore(MyCaseFrontListDto listDto, Model model) {
        list(listDto, model);
        return "front/cmc/case/case_list_item";
    }

    //案件详情
    @RequestMapping("/info.htm")
    public String info(@RequestParam("uuid") String uuid, Model model) {
        CaseFrontInfoDto info = service.loadFrontInfo(uuid);
        model.addAttribute("info", info);
        return "front/cmc/case/info";
    }

    //添加案件留言
    @RequestMapping("/comment")
    @ResponseBody
    public JsonDto addComment(@Valid CaseCommentFormDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return BindingResultUtils.buildErrorMessage(result, new JsonDto());
        }
        return service.submitFrontComments(formDto);
    }

}
